package mn.slide.laborberlin.main;

import mn.slide.laborberlin.adaptor.EinExtendableAdapter;
import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract.RawContacts.Data;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EintragActivity extends Activity {
	ExpandableListView list;
	Button back_btn;
	private String[] mStrings = {};
	LinearLayout entProg;

	EditText search_edt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eintrags);
		View footerView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.ein_header, null, false);

		list = (ExpandableListView) findViewById(R.id.expandableListView1);
		list.addHeaderView(footerView);
		list.setGroupIndicator(null);
		entProg = (LinearLayout) findViewById(R.id.entragProg);
		int position = getIntent().getExtras().getInt("position");
		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					String k = search_edt.getText().toString();
					if (k.length() > 0) {

						Intent intent = new Intent(EintragActivity.this,
								SearchActivity.class);
						intent.putExtra("searchTxt", k);
						HomeGroupActivity.group.goTo(intent);

					}
					return true;
				}
				return false;
			}
		});
		new ReadData(this, position).execute();
	}

	@Override
	public void onBackPressed() {
		GroupStarter.getInstance().onBackpressed(this);
	}

	private class ReadData extends AsyncTask<Void, Void, String[]> {
		protected int position;
		protected Activity context;
		protected EinExtendableAdapter adaptor;

		public ReadData(Activity context, int position) {
			this.position = position;
			this.context = context;
		}

		@Override
		protected String[] doInBackground(Void... params) {

			adaptor = new EinExtendableAdapter(context, position);

			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {
			super.onPostExecute(result);
			TextView header = (TextView) findViewById(R.id.eintrag_header_tv);
			if (position != -1) {
				header.setText(DataLayer.layer.getKapitelList().get(position)
						.getValue());
				DataLayer.layer.kapitelName = 0;
			} else {
				header.setText("A-Z");
				DataLayer.layer.kapitelName = 1;
			}
			list.setAdapter(adaptor);
			for (int i = 0; i < adaptor.getGroupCount(); i++) {
				list.expandGroup(i);
			}
			entProg.setVisibility(View.GONE);
		}
	}
}
